package com.example.mushroom.controller;

import com.example.mushroom.entity.User;
import com.example.mushroom.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private UserService userService;  // 注入接口

    @PostMapping
    public int insertUser(@RequestBody User user) {
        return userService.insertUser(user);
    }

    @GetMapping("/{id}")
    public User selectUserById(@PathVariable Integer id) {
        return userService.selectUserById(id);
    }

    @GetMapping
    public List<User> selectAllUsers() {
        return userService.selectAllUsers();
    }

    @GetMapping("/username/{username}")
    public User selectUserByUsername(@PathVariable String username) {
        return userService.selectUserByUsername(username);
    }

    @PutMapping
    public int updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    @DeleteMapping("/{id}")
    public int deleteUserById(@PathVariable Integer id) {
        return userService.deleteUserById(id);
    }

    @GetMapping("/page")
    public List<User> selectUsersByPage(@RequestParam int offset, @RequestParam int limit) {
        return userService.selectUsersByPage(offset, limit);
    }
}